<template>
    <a-modal title="设置sprint信息" :bodyStyle="{ padding: 0 }" centered v-model:visible="visible" :width="600" v-if="visible">
        <div style="padding: 32px">
            <a-radio-group v-model:value="type" style="width: 100%">
                <a-radio :style="radioStyle" value="0">选择已有sprint</a-radio>
                <div class="margin-t-8">
                    <a-form layout="inline">
                        <a-row>
                            <a-col :span="20">
                                <a-form-item label="选择sprint">
                                    <h-search-select-cell :default-value="detailData.jiraSprintName" :disabled="type !== '0'" ref="searchSelectRef" type="versionSprint" label="name" :searchQuery="{ empId: userInfo.employeeDTO.empId, systemId: detailData.requireSystem || detailData.requestSystem, field: 'sprint' }" @success="handleSelectChange" @error="handleSprintError"> </h-search-select-cell>
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </div>
                <a-radio :style="radioStyle" value="1">新建sprint</a-radio>
                <div class="margin-t-8">
                    <a-form layout="inline">
                        <a-row>
                            <a-col :span="20">
                                <a-form-item label="新建sprint">
                                    <a-input :disabled="type !== '1'" placeholder="请输入名称限制30个字" v-model:value="form.name" :maxlength="30" />
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </div>
            </a-radio-group>
        </div>
        <template #footer>
            <flexbox justify="flex-end" class="btn-group">
                <a-button type="primary" @click="handleSubmit">保存设置</a-button>
            </flexbox>
        </template>
    </a-modal>
</template>

<script lang="ts">
import store from '@/store';
import api from '@/api';
import { useMessage } from '@/hooks/common/useMessage';
import { HSearchSelectCell } from '@/components';
import { defineComponent, ref, unref, reactive, toRefs } from 'vue';
import { message } from 'ant-design-vue/es';
export default defineComponent({
    components: {
        HSearchSelectCell
    },
    emits: ['success'],
    setup(_, { emit }) {
        const { createJiraLogin } = useMessage();
        const searchSelectRef = ref<any>();
        const state = reactive<Recordable>({
            type: '0',
            visible: false,
            detailData: {},
            checkedData: {},
            form: {
                name: ''
            },
            radioStyle: {
                display: 'block',
                height: '30px',
                lineHeight: '30px'
            }
        });
        const open = (detailData) => {
            state.detailData = detailData;
            state.form = {
                name: ''
            };
            state.type = '0';
            state.checkedData = {};
            state.visible = true;
        };
        const handleSubmit = async () => {
            if (state.type === '0') {
                emit('success', state.checkedData);
                state.visible = false;
            } else {
                const formData = new FormData();
                formData.append('empId', store.getters.userInfo.employeeDTO.empId);
                formData.append('name', state.form.name);
                formData.append('systemId', state.detailData.requireSystem || state.detailData.requestSystem);
                const { success, moule, errorMsg } = await api.jiraModel.createSprintByName(formData);
                if (success) {
                    emit('success', moule);
                    state.visible = false;
                } else {
                    message.error(errorMsg);
                }
            }
            state.checkedData = {};
        };
        const handleSprintError = ({ errorCode }) => {
            if (errorCode === '003') {
                createJiraLogin({
                    systemId: state.detailData.requireSystem,
                    onOk() {
                        unref(searchSelectRef).getOptions();
                    }
                });
                return;
            }
        };
        // 通用选择回调
        const handleSelectChange = (val) => {
            if (val.id) {
                state.checkedData = { ...val };
            }
        };
        return {
            searchSelectRef,
            ...toRefs(state),
            open,
            handleSubmit,
            handleSelectChange,
            handleSprintError
        };
    }
});
</script>